package user

import (
	"api-go/model"
	"api-go/serializer"
	"encoding/json"
	"strconv"
)

type Like struct {
	Context      string `form:"context"`
	PlaylistList string `form:"playlistList"`
	SongList     string `form:"songList"`
}

//插入收藏歌单
func (service *Like) InsertLikePlaylist(uid uint) *serializer.Response {

	//解析string，将其转换成json
	bodyMapData := []map[string]interface{}{}
	err := json.Unmarshal([]byte(service.Context), &bodyMapData)
	if err != nil {
		return serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	//将用户Id转为string类型
	uidString := strconv.FormatUint(uint64(uid), 10)

	var ran []*model.LikePlaylist

	//查询该用户有哪些已经上传的歌单，防止重复上传
	re := model.ExistPlaylist(uidString)

	//将每个数组的内容进行转换
First:
	for _, v := range bodyMapData {

		//部分id为float64类型，需要转为string
		if _, ok := v["id"].(float64); ok {
			v["id"] = strconv.FormatFloat(v["id"].(float64), 'f', -1, 64)
		}

		//判断当前数据库是否已经有该歌单；如果有就结束当前循环，没有就继续执行
		for _, k := range re {
			if k.Pid == v["id"].(string) && k.Platform == v["platform"].(string) {
				continue First
			}
		}
		//部分导入歌单没有该信息，所以需要判断一下
		if v["isImport"] == nil {
			v["isImport"] = false
		}
		newContext := &model.LikePlaylist{
			Uid:      uidString,
			Pid:      v["id"].(string),
			Platform: v["platform"].(string),
			PicUrl:   v["picUrl"].(string),
			Name:     v["name"].(string),
			IsImport: v["isImport"].(bool),
		}

		ran = append(ran, newContext)

	}

	if err = model.DB.CreateInBatches(ran, 100).Error; err != nil {
		return serializer.ErrorResponse(serializer.CodeUserNotExistError)
	}

	return serializer.OkResponse(nil)
}

//插入收藏音乐
func (service *Like) InsertLikeSong(uid uint) *serializer.Response {

	//解析string，将其转换成json
	bodyMapData := []map[string]interface{}{}
	err := json.Unmarshal([]byte(service.Context), &bodyMapData)
	if err != nil {
		return serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	//将用户Id转为string类型
	uidString := strconv.FormatUint(uint64(uid), 10)

	var ran []*model.LikeSong

	//查询该用户有哪些已经上传的歌曲，防止重复上传
	re := model.ExistSong(uidString)

	//将每个数组的内容进行转换
First:
	for _, v := range bodyMapData {

		//部分导入歌单没有该信息，所以需要判断一下
		if v["likePlaylist"] == nil {
			v["likePlaylist"] = "local"
		}

		if v["hash"] == nil {
			v["hash"] = ""
		}

		if v["album"] == nil {
			v["album"] = ""
		}

		//部分id为float64类型，需要转为string
		if _, ok := v["id"].(float64); ok {
			v["id"] = strconv.FormatFloat(v["id"].(float64), 'f', -1, 64)
		}

		//判断当前数据库是否已经有该歌单；如果有就结束当前循环，没有就继续执行
		for _, k := range re {
			if k.Sid == v["id"].(string) && k.Platform == v["platform"].(string) && k.Pid == v["likePlaylist"].(string) {
				continue First
			}
		}

		newContext := &model.LikeSong{
			Uid:      uidString,
			Sid:      v["id"].(string),
			Pid:      v["likePlaylist"].(string),
			Platform: v["platform"].(string),
			Name:     v["name"].(string),
			Singer:   v["singer"].(string),
			Hash:     v["hash"].(string),
			Album:    v["album"].(string),
		}

		ran = append(ran, newContext)

	}

	if err = model.DB.CreateInBatches(ran, 100).Error; err != nil {
		return serializer.ErrorResponse(serializer.CodeUserNotExistError)
	}

	return serializer.OkResponse(nil)
}

//获取当前账户已上传的歌单
func (service *Like) GetPlaylistByUserId(uid uint) *serializer.Response {
	//将用户Id转为string类型
	uidString := strconv.FormatUint(uint64(uid), 10)

	//查询该用户有哪些已经上传的歌单，防止重复上传
	re := model.ExistPlaylist(uidString)

	if len(re) == 0 {
		return serializer.ErrorResponse(serializer.CodeNoResult)
	}

	var ran []map[string]interface{}

	//对结果进行序列化，否则前端使用不了
	for _, k := range re {
		newContext := map[string]interface{}{
			"id":       k.Pid,
			"platform": k.Platform,
			"picUrl":   k.PicUrl,
			"isImport": k.IsImport,
			"name":     k.Name,
		}
		ran = append(ran, newContext)
	}
	return serializer.OkResponse(ran)
}

//获取当前账户已上传的歌曲
func (service *Like) GetSongByUserId(uid uint) *serializer.Response {
	//将用户Id转为string类型
	uidString := strconv.FormatUint(uint64(uid), 10)

	//查询该用户有哪些已经上传的歌单，防止重复上传
	re := model.ExistSong(uidString)

	if len(re) == 0 {
		return serializer.ErrorResponse(serializer.CodeNoResult)
	}

	var ran []map[string]interface{}

	//对结果进行序列化，否则前端使用不了
	for _, k := range re {
		newContext := map[string]interface{}{
			"id":           k.Sid,
			"likePlaylist": k.Pid,
			"platform":     k.Platform,
			"name":         k.Name,
			"singer":       k.Singer,
			"hash":         k.Hash,
			"album":        k.Album,
		}
		ran = append(ran, newContext)
	}

	return serializer.OkResponse(ran)
}

//删除全部歌单
func (service *Like) DeleteAllPlaylist(uid uint) *serializer.Response {
	//将用户Id转为string类型
	uidString := strconv.FormatUint(uint64(uid), 10)

	//执行删除操作
	model.DeleteAllPlaylist(uidString)

	return serializer.OkResponse(nil)
}

//删除全部歌曲
func (service *Like) DeleteAllSong(uid uint) *serializer.Response {
	//将用户Id转为string类型
	uidString := strconv.FormatUint(uint64(uid), 10)

	//执行删除操作
	model.DeleteAllSong(uidString)

	return serializer.OkResponse(nil)
}

//将插入收藏歌单和音乐接口合并
func (service *Like) InsertLikeList(uid uint) *serializer.Response {

	if service.PlaylistList == "" {
		return serializer.ErrorResponse(serializer.CodeIsNullOfUpContext)
	}

	if service.SongList == "" {
		return serializer.ErrorResponse(serializer.CodeIsNullOfUpContext)
	}

	//将用户Id转为string类型
	uidString := strconv.FormatUint(uint64(uid), 10)

	//首先解析歌单

	//解析string，将其转换成json
	bodyMapPlaylist := []map[string]interface{}{}
	err := json.Unmarshal([]byte(service.PlaylistList), &bodyMapPlaylist)
	if err != nil {
		return serializer.ErrorResponse(serializer.CodeUnknownError)
	}

	var ran []*model.LikePlaylist

	//查询该用户有哪些已经上传的歌单，防止重复上传
	re := model.ExistPlaylist(uidString)

	//将每个数组的内容进行转换
First:
	for _, v := range bodyMapPlaylist {

		//部分id为float64类型，需要转为string
		if _, ok := v["id"].(float64); ok {
			v["id"] = strconv.FormatFloat(v["id"].(float64), 'f', -1, 64)
		}

		//判断当前数据库是否已经有该歌单；如果有就结束当前循环，没有就继续执行
		for _, k := range re {
			if k.Pid == v["id"].(string) && k.Platform == v["platform"].(string) {
				continue First
			}
		}
		//部分导入歌单没有该信息，所以需要判断一下
		if v["isImport"] == nil {
			v["isImport"] = false
		}
		newContext := &model.LikePlaylist{
			Uid:      uidString,
			Pid:      v["id"].(string),
			Platform: v["platform"].(string),
			PicUrl:   v["picUrl"].(string),
			Name:     v["name"].(string),
			IsImport: v["isImport"].(bool),
		}

		ran = append(ran, newContext)

	}

	if err = model.DB.CreateInBatches(ran, 100).Error; err != nil {
		return serializer.ErrorResponse(serializer.CodeUserNotExistError)
	}

	//上传喜欢的音乐

	//解析string，将其转换成json
	bodyMapSong := []map[string]interface{}{}
	err = json.Unmarshal([]byte(service.SongList), &bodyMapSong)
	if err != nil {
		return serializer.ErrorResponse(serializer.CodeUnknownError)
	}

	var rans []*model.LikeSong

	//查询该用户有哪些已经上传的歌曲，防止重复上传
	ree := model.ExistSong(uidString)

	//将每个数组的内容进行转换
Secord:
	for _, v := range bodyMapSong {

		//部分导入歌单没有该信息，所以需要判断一下
		if v["likePlaylist"] == nil {
			v["likePlaylist"] = "local"
		}

		if v["hash"] == nil {
			v["hash"] = ""
		}

		if v["album"] == nil {
			v["album"] = ""
		}

		//部分id为float64类型，需要转为string
		if _, ok := v["id"].(float64); ok {
			v["id"] = strconv.FormatFloat(v["id"].(float64), 'f', -1, 64)
		}

		//判断当前数据库是否已经有该歌单；如果有就结束当前循环，没有就继续执行
		for _, k := range ree {
			if k.Sid == v["id"].(string) && k.Platform == v["platform"].(string) && k.Pid == v["likePlaylist"].(string) {
				continue Secord
			}
		}

		newContext := &model.LikeSong{
			Uid:      uidString,
			Sid:      v["id"].(string),
			Pid:      v["likePlaylist"].(string),
			Platform: v["platform"].(string),
			Name:     v["name"].(string),
			Singer:   v["singer"].(string),
			Hash:     v["hash"].(string),
			Album:    v["album"].(string),
		}

		rans = append(rans, newContext)

	}

	if err = model.DB.CreateInBatches(rans, 100).Error; err != nil {
		return serializer.ErrorResponse(serializer.CodeUserNotExistError)
	}

	return serializer.OkResponse(nil)
}

//将获取的接口合并
func (service *Like) GetLikeList(uid uint) *serializer.Response {
	//将用户Id转为string类型
	uidString := strconv.FormatUint(uint64(uid), 10)

	//查询该用户有哪些已经上传的歌单，防止重复上传
	re := model.ExistPlaylist(uidString)

	if len(re) == 0 {
		return serializer.ErrorResponse(serializer.CodeNoResult)
	}

	var ran []map[string]interface{}

	//对结果进行序列化，否则前端使用不了
	for _, k := range re {
		newContext := map[string]interface{}{
			"id":       k.Pid,
			"platform": k.Platform,
			"picUrl":   k.PicUrl,
			"isImport": k.IsImport,
			"name":     k.Name,
		}
		ran = append(ran, newContext)
	}

	//查询该用户有哪些已经上传的歌单，防止重复上传
	ree := model.ExistSong(uidString)

	if len(ree) == 0 {
		return serializer.ErrorResponse(serializer.CodeNoResult)
	}

	var rans []map[string]interface{}

	//对结果进行序列化，否则前端使用不了
	for _, v := range ree {
		newContext := map[string]interface{}{
			"id":           v.Sid,
			"likePlaylist": v.Pid,
			"platform":     v.Platform,
			"name":         v.Name,
			"singer":       v.Singer,
			"hash":         v.Hash,
			"album":        v.Album,
		}
		rans = append(rans, newContext)
	}
	result := map[string]interface{}{
		"playlist": ran,
		"song":     rans,
	}
	return serializer.OkResponse(result)
}
